﻿package Leophy.Net {
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLRequest;

	/**
	 * ...
	 * @author TOPHY
	 */
	public class XMLLoader extends URLLoader {

		public static var COMPLETE_XML : String = "onCompleteXML";

		public static function get ignoreComments() : Boolean {
			return XML.ignoreComments;
		}

		public static function get ignoreProcessingInstructions() : Boolean {
			return XML.ignoreProcessingInstructions;
		}

		public static function get ignoreWhitespace() : Boolean {
			return XML.ignoreWhitespace;
		}

		public static function set ignoreComments(value : Boolean) : void {
			XML.ignoreComments = value;
		}

		public static function set ignoreProcessingInstructions(value : Boolean) : void {
			XML.ignoreProcessingInstructions = value;
		}

		public static function set ignoreWhitespace(value : Boolean) : void {
			XML.ignoreWhitespace = value;
		}

		public function XMLLoader() {
			this.addEventListener(Event.COMPLETE, loadedxml);
		}

		/**
		 * 加载XML（请注册 XMLLoader.COMPLETE_XML 事件）
		 * @param	url 地址
		 */
		public function loadXML(url : String) : void {
			ignoreComments = true;
			ignoreProcessingInstructions = true;
			ignoreWhitespace = true;
			var urlrquest : URLRequest = new URLRequest(url);
			super.load(urlrquest);
		}

		private function loadedxml(ev : Event) : void {
			var rs : XML = XML(ev.target.data);
			var e : Event = new Event(COMPLETE_XML);
			this.data = rs;
			this.dataFormat = typeof(rs);
			this.dispatchEvent(e);
		}
	}
}